package com.hznu.ez4life.modules.controller;


import com.hznu.ez4life.modules.entity.Needitem;
import com.hznu.ez4life.modules.service.NeeditemService;
import com.hznu.ez4life.myBeans.NeedItemSearch;
import com.hznu.ez4life.myBeans.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DragonDoor
 * @since 2019-07-25
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/needitem")
public class NeeditemController {

    @Autowired
    NeeditemService needitemService;

    @PostMapping("/addOne")
    public R addOne(@RequestBody Needitem needitem)
    {
        try
        {
            needitemService.insert(needitem);
            return R.ok();
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("findListByUserId/{userId}")
    public R findListByUserId(@PathVariable Integer userId)
    {
        try
        {
            return R.ok().put("needItemList",needitemService.findListByUserId(userId));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("findBatchByCondition")
    public R findBatchByCondition(@RequestBody NeedItemSearch needItemSearch)
    {
        try
        {
            return R.ok().put("needItemList",needitemService.findBatchByCondition(needItemSearch));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Integer id)
    {
        try
        {
            return R.ok().put("needItem",needitemService.findById(id));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }
}

